package com.nlp.visualization.utils;




import com.nlp.visualization.exception.AenrolException;

import java.io.*;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;

public class StringUtils {

  public static boolean isEmpty(String s) {
    boolean result = s == null || s.trim().length() == 0 || s.trim().equals("undefined");
    if (result) {
      Info.print(s + " 为空");
    }
    return result;
  }

  public static String encodeUrl(String str) throws UnsupportedEncodingException {
    return URLEncoder.encode(str, "utf-8");
  }

  public static String decodeUrl(String str) throws UnsupportedEncodingException {
    // return URLDecoder.decode(str.replaceAll("%2B", "+").replaceAll("%3D",
    // "="), "utf-8");
    return URLDecoder.decode(str, "utf-8");
  }

  public static String getParentLongNumber(String selfLongNumber, String splitChar) {
    if (!isEmpty(selfLongNumber) && selfLongNumber.split(splitChar).length > 0) {
      String[] ss = selfLongNumber.split(splitChar);
      StringBuffer parentLongNumber = new StringBuffer();
      for (int i = 0, len = ss.length - 1; i < len; i++) {
        parentLongNumber.append((i == 0 ? "" : splitChar) + ss[i]);
      }
      return parentLongNumber.toString();
    } else {
      return null;
    }
  }

  synchronized public static String createId() {
    return UUID.randomUUID().toString().replaceAll("-", "");
  }

  public static byte[] getBytes(char[] chars) {
    Charset cs = Charset.forName("UTF-8");
    CharBuffer cb = CharBuffer.allocate(chars.length);
    cb.put(chars);
    cb.flip();
    ByteBuffer bb = cs.encode(cb);
    return bb.array();
  }

  public static char[] getChars(byte[] bytes) {
    Charset cs = Charset.forName("UTF-8");
    ByteBuffer bb = ByteBuffer.allocate(bytes.length);
    bb.put(bytes);
    bb.flip();
    CharBuffer cb = cs.decode(bb);
    return cb.array();
  }

  public static String catHqlInString(String[] strings) throws Exception {
    if (strings.length == 0) {
      throw new AenrolException("没有要连接的内容");
    }
    StringBuffer sb = new StringBuffer("(");
    for (int i = 0; i < strings.length; i++) {
      if (i > 0) {
        sb.append(",");
      }
      sb.append("'" + strings[i] + "'");
    }
    sb.append(")");
    return sb.toString();
  }

  public static String catHqlInString(Collection<String> strings) throws Exception {
    return catHqlInString(strings, true);
  }

  public static String catHqlInString(Collection<String> strings, boolean isNeedBrackets)
      throws Exception {
    if (strings.size() == 0) {
      throw new AenrolException("没有要连接的内容");
    }
    StringBuffer sb = new StringBuffer();
    if (isNeedBrackets) {
      sb.append("(");
    }
    Iterator<String> it = strings.iterator();
    boolean isFirst = true;
    while (it.hasNext()) {
      String id = it.next();
      if (isFirst) {
        isFirst = false;
      } else {
        sb.append(",");
      }
      sb.append("'" + id + "'");
    }
    if (isNeedBrackets) {
      sb.append(")");
    }
    return sb.toString();
  }

  public static String formatThrowableStackInfo(Throwable e) {
    StackTraceElement[] els = e.getStackTrace();
    StringBuffer msg = new StringBuffer(formatThrowableInfo(e));
    for (StackTraceElement el : els) {
      msg.append(
          "at [" + el.getClassName() + "." + el.getMethodName() + ", in source " + el.getFileName()
              + ",line " + el.getLineNumber() + "]<br/>");
    }
    return msg.toString();
  }

  public static String formatThrowableInfo(Throwable e) {
    StringBuffer sb = new StringBuffer(e.toString() + "<br/>");
    if (e.getCause() != null) {
      sb.append("caused by " + e.getCause().toString() + "<br/>");
    }
    return sb.toString();
  }

  /**
   * 字符串List连接成一个String
   */
  public static String concatStringList(List<String> strList) {
    StringBuffer sb = new StringBuffer();
    for (String str : strList) {
      sb.append(str);
    }
    return sb.toString();
  }

  public static void main(String[] args) {
    try {
      System.out.println(encodeUrl("+"));
    } catch (UnsupportedEncodingException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }

  public static boolean writeFileToString(String fileNmae, String content) {
    // file(内存)----输入流---->【程序】----输出流---->file(内存)
    File file = new File(fileNmae);
    try {
      file.createNewFile(); // 创建文件
    } catch (IOException e) {
      e.printStackTrace();
    }
    // 向文件写入内容(输出流)
    byte bt[] = new byte[1024];
    bt = content.getBytes();
    try {
      FileOutputStream in = new FileOutputStream(file);
      try {
        in.write(bt, 0, bt.length);
        in.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    }
    return false;
  }
}
